package com.vanke.agent.common;

import java.io.Serializable;

import javax.jms.DeliveryMode;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;

import com.vanke.agent.Cfg;

public abstract class QueueMsgTemplate {
	
	private static final Logger log=Logger.getLogger(QueueMsgTemplate.class);
	
	// tcp 地址
	public static final String BROKER_URL = Cfg.get("mq.addr");

	/**默认目标队列**/
	public static final String DESTINATION = Cfg.get("mq.notice");
	
	/**目标队列**/
	private String destination;
	
	/**实体对象消息内容**/
	private Serializable message;
	
	/**基本数据类型消息内容**/
	private Object obj;
	
	private String messageType;

	/**
	 * <b>function:</b> 发送队列消息
	 * @param session
	 * @param producer
	 * @throws Exception
	 */
	public abstract void sendMessage(QueueSession session, QueueSender sender) throws Exception;
	QueueConnectionFactory factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, BROKER_URL);
	
	public final void send() throws Exception {

        QueueConnection connection = null;
        QueueSession session = null;
        try {

            // 创建链接工厂
            // 通过工厂创建一个连接
            connection = factory.createQueueConnection();
            // 启动连接
            connection.start();
            // 创建一个session会话
            session = connection.createQueueSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
            // 创建一个消息队列
            Queue queue = session.createQueue(getDestination());
            // 创建消息发送者
            javax.jms.QueueSender sender = session.createSender(queue);
            // 设置持久化模式
            sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            sendMessage(session, sender);
            // 提交会话
            session.commit();
            if(this.getMessage()!=null){
            	log.info("发送实体对象队列消息成功；消息对象："+this.getMessage());
            }
            if(this.getObj()!=null){
            	log.info("发送map对象队列消息成功；消息对象："+this.getObj());
            }

        } catch (Exception e) {
        	log.info("发送队列消息失败:"+e.getStackTrace().toString());
        } finally {
            // 关闭释放资源
            if (session != null) {
                session.close();
            }
            if (connection != null) {
                connection.close();
            }
        }
	}

	public Serializable getMessage() {
		return message;
	}

	public void setMessage(Serializable message) {
		this.message = message;
	}
	
	/**
	 * 获取目标队列；如果没有就取默认队列： vanke.mq.queue
	 * @return
	 */
	public String getDestination() {
		if(destination ==null || destination.equals("")){
			return DESTINATION;
		}
		return destination;
	}
	
	/**
	 * 设置目标队列
	 * @param destination
	 */
	public void setDestination(String destination) {
		this.destination = destination;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

	public String getMessageType() {
		return messageType;
	}

	public void setMessageType(String messageType) {
		this.messageType = messageType;
	}
	
}
